package com.abaaba.project2.service.serviceImpl;

import com.abaaba.project2.bean.FeedBack;
import com.abaaba.project2.bean.FeedBackExample;
import com.abaaba.project2.bean.data.BaseData;
import com.abaaba.project2.bean.vo.AdminFeedbackVo;
import com.abaaba.project2.mapper.FeedBackMapper;
import com.abaaba.project2.service.AdminFeedbackService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AdminFeedbackServiceImpl implements AdminFeedbackService {
    @Autowired
    FeedBackMapper feedBackMapper;

    @Override
    public BaseData getFeedbackList(Integer page, Integer limit, String username, String sort, String order, Integer id) {
        PageHelper.startPage(page, limit);
        FeedBackExample feedBackExample = new FeedBackExample();
        feedBackExample.setOrderByClause(sort + " " + order);

        FeedBackExample.Criteria criteria = feedBackExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (id != null) {
            criteria.andIdEqualTo(id);
        }
        if (username != null && !"".equals(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }

        List<FeedBack> feedBacks = feedBackMapper.selectByExample(feedBackExample);
        List<AdminFeedbackVo> adminFeedbackVos = new ArrayList<>();
        for (FeedBack feedBack : feedBacks) {
            // 如果该条记录有图片，则获取图片 url 列表
            AdminFeedbackVo adminFeedbackVo = new AdminFeedbackVo(feedBack.getId(), feedBack.getUserId(), feedBack.getUsername(), feedBack.getMobile(),
                    feedBack.getFeedType(), feedBack.getContent(), feedBack.getStatus(), feedBack.getHasPicture(),
                    new String[0], feedBack.getAddTime(), feedBack.getUpdateTime(), feedBack.getDeleted());
            if (feedBack.getHasPicture()) {
                String picUrls = feedBack.getPicUrls();
                String[] https = picUrls.split("  ");
                adminFeedbackVo.setPicUrls(https);
//                String[] picUrlsList = new String[https.length];
//                for (int i = 0; i < https.length; i++) {
//                    if (https[i] != null && !"".equals(https[i])) {
//                        picUrlsList[i] = https[i];
//                    }
//                }
            }

            adminFeedbackVos.add(adminFeedbackVo);
        }
        PageInfo<FeedBack> feedBackPageInfo = new PageInfo<>(feedBacks);
        long total = feedBackPageInfo.getTotal();

        BaseData<AdminFeedbackVo> feedBackBaseData = new BaseData<>();
        feedBackBaseData.setTotal(total);
        feedBackBaseData.setItems(adminFeedbackVos);
        return feedBackBaseData;
    }
}
